package com.gpay.erp.web.control;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContext;
import com.gpay.erp.core.dao.page.PageHelper;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.core.system.bean.Suppliers;
import com.gpay.erp.services.system.SuppliersInteface;
import com.gpay.erp.web.util.WebUtil;
import com.gpay.erp.web.vo.ResponseInfo;
import com.gpay.erp.web.vo.UserSessionInfor;

/**
 * 会员管理（客户/供应商管理）
 * 
 * @author lg
 * 
 */
@Controller
@RequestMapping("/suppliersController/")
public class SuppliersController extends BaseController {
	private Log log = LogFactory.getLog(SuppliersController.class);
	@Autowired
	private SuppliersInteface suppliersService;

	// 页面跳转 会员管理
	@RequestMapping("supSelect")
	public String supSelect() {
		return "system/suppliers/supSelect";
	}

	// 页面跳转 会员新增
	@RequestMapping("supAdd")
	public String supAdd() {
		return "system/suppliers/supAdd";
	}

	// 页面跳转 会员修改
	@RequestMapping("supUpdate")
	public String supUpdate() {
		return "system/suppliers/supUpdate";
	}

	/*
	 * 点击树型菜单--会员管理，进行分页查询
	 * 
	 * @param pageHelper
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("selectSuppliers")
	public void selectsuppliers(PagerModel pagerModel, Suppliers suppliers,
			PageHelper pageHelper, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/* 当前页 */
		int pagenow = pageHelper.getPage();
		/* 每页显示记录数 */
		int rows = pageHelper.getRows();
		/* 分页信息填充进入PagerModel中 */
		suppliers.setPageSize(rows);
		suppliers.setFromRow((pagenow - 1) * rows);
		suppliers.setToRow(pagenow * rows);
		/* 分页查询，返回PagerModel */
		pagerModel = suppliersService.selectPageList(suppliers);
		if (pagerModel.getRows().size() > 0) {
			responseInfo.setResult("00010");
			responseInfo
					.setMessage(requestContext.getMessage("select.success"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击新增，增加会员信息
	 * 
	 * @param suppliers
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("addSuppliers")
	public void addsuppliers(Suppliers suppliers, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/*
		 * suppliers.setMemberNo("0001安心"); suppliers.setMemberName("武汉呼呼");
		 * suppliers.setTfn("sasasdad");
		 */
		int id = suppliersService.insert(suppliers);
		if (id != 0) {
			log.info("插入记录后的id值为：" + id);
			responseInfo.setResult("00040");
			responseInfo.setMessage(requestContext.getMessage("add.success"));
		} else {
			responseInfo.setResult("00041");
			responseInfo.setMessage(requestContext.getMessage("add.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击明细，根据ID查看会员详细信息
	 * 
	 * @param suppliers
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("detailSuppliers")
	public void detailSuppliers(PagerModel pagerModel, Suppliers suppliers,
			HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/* suppliers.setId("3"); */
		if (suppliers.getId() != null)// 获取前端传入的id,判断是否为null
		{
			pagerModel = suppliersService.selectById(suppliers.getId());// 根据ID查询明细
			responseInfo.setResult("00010");
			responseInfo
					.setMessage(requestContext.getMessage("select.success"));
		} else {
			responseInfo.setResult("00011");
			responseInfo.setMessage(requestContext.getMessage("select.fail"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击修改，修改会员信息
	 * 
	 * @param suppliers
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("updateSuppliers")
	public void updatesuppliers(Suppliers suppliers,
			HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/*
		 * suppliers.setId("3"); suppliers.setMemberNo("0001");
		 * suppliers.setMemberName("武汉飞中"); suppliers.setTfn("123145");
		 * suppliers.setAbbrName("FZ");
		 */
		int id = suppliersService.update(suppliers);
		if (id != 0) {
			log.info("修改记录的id值为：" + id);
			responseInfo.setResult("00030");
			responseInfo
					.setMessage(requestContext.getMessage("update.success"));
		} else {
			responseInfo.setResult("00031");
			responseInfo.setMessage(requestContext.getMessage("update.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击删除，根据id删除会员信息
	 * 
	 * @param ids[]
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("deleteSuppliers")
	public void deletesuppliers(HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		HttpSession session = request.getSession();
		String[] ids = request.getParameterValues("ids");// 接收前端传输的id值
		/* String [] ids={"3"}; */
		if (ids != null) {
			int dels = suppliersService.deletes(ids);
			if (dels != 0) {
				responseInfo.setResult("00020");
				responseInfo.setMessage(requestContext
						.getMessage("delete.success"));
			} else {
				responseInfo.setResult("00021");
				responseInfo.setMessage(requestContext
						.getMessage("delete.fail"));
			}
		} else {
			responseInfo.setResult("10111");
			responseInfo.setMessage(requestContext.getMessage("no.data.jsp"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	// 查询所有客户信息
	@RequestMapping("selectList")
	public void selectList(PagerModel pagerModel, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		List<Suppliers> selectList = suppliersService.selectList(pagerModel);
		if (selectList.size() > 0) {
			responseInfo.setResult("00040");
			responseInfo
					.setMessage(requestContext.getMessage("select.success"));
		}
		responseInfo.setObject(selectList);
		WebUtil.responseToJson(response, responseInfo);
	}
}
